<!--提货卡规则添加-->
<template>
  <div class="form-submit">
    <div class="i-layout-page-header">
      <PageHeader class="product_tabs" hidden-breadcrumb>
        <div slot="title">
          <router-link
              :to="{ path: '/admin/user/recharcard/rule' }"
          >
            <div class="font-sm after-line">
              <span class="iconfont iconfanhui"></span>
              <span class="pl10">返回</span>
            </div>
          </router-link>
          <span
              v-text="id != 0 ? '编辑提货卡规则' : '添加提货卡规则'"
              class="mr20 ml16"
          ></span>
        </div>
      </PageHeader>
    </div>
    <Card :bordered="false" dis-hover class="ivu-mt">
      <Form :model="formData" :label-width="150">

        <FormItem label="提货卡规则名称" required>
          <Input
              v-model="formData.title"
              v-width="320"
              placeholder="请输入提货卡规则名称"
          ></Input>
        </FormItem>

        <FormItem label="提货卡规则内容" required>
          <Input v-model="formData.content " type="textarea" :autosize="{minRows: 2,maxRows: 8}" placeholder="请输入提货卡使用规则内容"></Input>
        </FormItem>

        <FormItem label="是否开启" required>
          <i-switch
              v-model="formData.status"
              :true-value="1"
              :false-value="0"
              size="large"
          >
            <span slot="open">开启</span>
            <span slot="close">关闭</span>
          </i-switch>

        </FormItem>
      </Form>
    </Card>
    <Card
        :bordered="false"
        dis-hover
        class="fixed-card"
        :style="{ left: `${!menuCollapse ? '200px' : isMobile ? '0' : '80px'}` }"
    >
      <Form>
        <FormItem>
          <Button type="primary" @click="save" :disabled="disabled"
          >{{id != 0 ? '立即修改' : '立即创建'}}</Button
          >
        </FormItem>
      </Form>
    </Card>
  </div>
</template>

<script>
import { mapState } from "vuex";
import {  recharcardRuleSaveApi,  recharcardRuleDetailApi,  recharcardRuleSavePut } from "@/api/user.js";
export default {
  name: "rule",
  components: {
  },
  data() {
    return {
      props: { multiple: true },

      disabled: false,
      time:'',
      time2:'',
      formData: {
        title: '', //提货卡规则名称
        status : 0, //状态
        content : '', //提货卡使用规则
      },
      gridPic: {
        xl: 6,
        lg: 8,
        md: 12,
        sm: 12,
        xs: 12
      },
      gridBtn: {
        xl: 4,
        lg: 8,
        md: 8,
        sm: 8,
        xs: 8
      },
      id:0
    };
  },
  computed: {
    ...mapState("admin/layout", ["isMobile", "menuCollapse"]),
  },
  created() {
    if (this.$route.query.id) {
      this.id = this.$route.query.id
      this.getCouponDetail();
    }
  },
  methods: {
    // 获取提货卡详情接口
    getCouponDetail() {
      recharcardRuleDetailApi(this.$route.query.id)
          .then((res) => {
            let data = res.data;
            this.formData.title = data.title;
            this.formData.content = data.content;
            this.formData.status = data.status;
            if (data.use_min_price !== "0.00") {
              this.isMinPrice = 1;
            }
          })
          .catch((err) => {
            this.$Message.error(err.msg);
          });
    },


    // 创建
    save() {
      if (this.formData.title == '') {
        return this.$Message.error("请输入提货卡规则名称");
      }
      if (this.formData.content == '') {
        return this.$Message.error("请输入提货卡规则内容");
      }
      this.disabled = false;
      if(this.id == 0){
        recharcardRuleSaveApi(this.formData)
            .then((res) => {
              this.disabled = true;
              this.$Message.success(res.msg);
              setTimeout(() => {
                this.$router.push({

                  path: "/admin/user/recharcard/rule",
                });
              }, 1000);
            })
            .catch((err) => {
              this.$Message.error(err.msg);
            });
      }else{
        this.formData.id = this.id
        recharcardRuleSavePut(this.formData).then(res=>{
          this.disabled = true;
          this.$Message.success('修改成功');
          setTimeout(() => {
            this.$router.push({
              // path: "/admin/recharcard/list",
              path: "/admin/user/recharcard/rule",
            });
          }, 1000);
        })
            .catch((err) => {
              this.$Message.error(err.msg);
            });
      }
    },
    change(){
      this.time = ''
      this.formData.recharcard_use_time = 0
    },
    //对象数组去重；
    unique(arr) {
      const res = new Map();
      return arr.filter(
          (arr) => !res.has(arr.product_id) && res.set(arr.product_id, 1)
      );
    },

    cancel() {
      this.modals = false;
    },

  },
};
</script>

<style scoped lang="stylus">
.picBox {
  display: inline-block;
  cursor: pointer;
}
.form .pictrue {
  width: 60px;
  height: 60px;
  border: 1px dotted rgba(0, 0, 0, 0.1);
  margin-right: 10px;
}

.form .pictrue img {
  width: 100%;
  height: 100%;
}
.productType {
  width: 200px;
  height: 60px;
  background: #FFFFFF;
  border-radius: 3px;
  border: 1px solid #E7E7E7;
  float: left;
  text-align: center;
  padding-top: 8px;
  position: relative;
  cursor: pointer;
  line-height: 23px;
  margin-right: 12px;

&.on {
   border-color: #1890FF;
 }

.name {
  font-size: 14px;
  font-weight: 600;
  color: rgba(0, 0, 0, 0.85);

&.on {
   color: #1890FF;
 }
}

.title {
  font-size: 12px;
  font-weight: 400;
  color: #999999;
}

.jiao {
  position: absolute;
  bottom: 0;
  right: 0;
  width: 0;
  height: 0;
  border-bottom: 26px solid #1890FF;
  border-left: 26px solid transparent;
}

.iconfont {
  position: absolute;
  bottom: -3px;
  right: 1px;
  color: #FFFFFF;
  font-size: 12px;
}
}

.info {
  color: #888;
  font-size: 12px;
}

.ivu-input-wrapper {
  width: 320px;
}

.ivu-input-number {
  width: 160px;
}

.ivu-date-picker {
  width: 320px;
}

.ivu-icon-ios-camera-outline {
  width: 58px;
  height: 58px;
  border: 1px dotted rgba(0, 0, 0, 0.1);
  border-radius: 4px;
  background-color: rgba(0, 0, 0, 0.02);
  line-height: 58px;
  cursor: pointer;
  vertical-align: middle;
}

.upload-list {
  width: 58px;
  height: 58px;
  border: 1px dotted rgba(0, 0, 0, 0.1);
  border-radius: 4px;
  margin-right: 15px;
  display: inline-block;
  position: relative;
  cursor: pointer;
  vertical-align: middle;
}

.upload-list img {
  display: block;
  width: 100%;
  height: 100%;
}

.ivu-icon-ios-close-circle {
  position: absolute;
  top: 0;
  right: 0;
  transform: translate(50%, -50%);
}

.form-submit {
/deep/.ivu-card {
  border-radius: 0;
}

margin-bottom: 79px;

.fixed-card {
  position: fixed;
  right: 0;
  bottom: 0;
  left: 200px;
  z-index: 99;
  box-shadow: 0 -1px 2px rgb(240, 240, 240);

/deep/ .ivu-card-body {
  padding: 15px 16px 14px;
}

.ivu-form-item {
  margin-bottom: 0;
}

/deep/ .ivu-form-item-content {
  margin-right: 124px;
  text-align: center;
}

.ivu-btn {
  height: 36px;
  padding: 0 20px;
}
}
}
</style>